//
//  ReviewVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/13.
//  Copyright © 2016年 阳光. All rights reserved.
//  审核界面

#import "ReviewVC.h"
#import "ModifyProfileVC.h"
#import "ConfirmVC.h"
#import "AppDelegate.h"
#import "QBMainVC.h"

@interface ReviewVC ()<UIScrollViewDelegate,CustomNavViewDelegate>
{
     BOOL isCN;//.
}

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)NSDictionary *doctorInfoDic;

@end



@implementation ReviewVC

// 返回跳转回哪个界面判断
-(void)leftBtnEvent
{
    // 来自认证界面跳转 返回进行连跳
    if ([_isConfirmType isEqualToString:@"isConfirmType"])
    {
        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
    }
    // 来自登录界面跳转 返回直接跳
    else
    {
         [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
    }
}

-(void)rightBtnEvent
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=MAIN_BACK_COLOR;
    
    isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];//.
    
    
    [self getUserInfoData];
    
}


-(void)getUserInfoData
{
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"]
                        };

    [QBRequest UserInfoWithDic:dic Success:^(id res)
     {
         _doctorInfoDic=res[@"info"];
         
         //NSLog(@"_doctorInfoDic :%@", _doctorInfoDic);
         
         [self initScrollView];
         
     } failure:^(NSString *error)
     {
         
     }];
}


-(void)initScrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=MAIN_BACK_COLOR;
    _scrollView.delegate=self;
    _scrollView.scrollEnabled=YES;
    _scrollView.userInteractionEnabled=YES;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,0);
    [_scrollView addSubview:_mainView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.mainView.frame));
    
}

#define cellH   kmargin*5
-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
    mainView.backgroundColor=MAIN_BACK_COLOR;
    
    NSString *confirmResultStr;
    if ([_auth_statusStr isEqualToString:@"1"])
    {
        confirmResultStr = NSLocalizedString(@"Reviewing", nil);//@"认证中"
    }
    else  if ([_auth_statusStr isEqualToString:@"2"])
    {
        confirmResultStr = NSLocalizedString(@"Renzheng Succeed", nil);//@"认证成功"
    }
    else  if ([_auth_statusStr isEqualToString:@"3"])
    {
        confirmResultStr = NSLocalizedString(@"Review Error", nil);//@"认证失败"
    }
    
    CustomNavView *loginNavView=[[CustomNavView alloc]initWithTitle:confirmResultStr leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];
    loginNavView.delegate=self;
    loginNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:loginNavView];
    
    setViewHeight_Add(mainView,64+kmargin);
    
/***** 头像 *****/
    // 头像
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*8)];
    headView.backgroundColor=WhiteColor;
    [mainView addSubview:headView];
    
     UILabel *headLabel=[mainView getLabelWithText:NSLocalizedString(@"Profile Photo", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [headView addSubview:headLabel];
    [headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(headView.mas_centerY);
    }];
    
    // 头像
    UIImageView *headUrl=[UIImageView new];
    [headView addSubview:headUrl];
     ViewBorderRadius(headUrl,128/4,1,ClearColor);
    [headUrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.size.mas_equalTo(CGSizeMake(128/2,128/2));
        make.centerY.mas_equalTo(headLabel.mas_centerY);
    }];
    [headUrl sd_setImageWithURL:[NSURL URLWithString:_doctorInfoDic[@"img"]] placeholderImage:[UIImage imageNamed:@"head"]];
    
    UIView *headLine=[UIView new];
    [headView addSubview:headLine];
    [headLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    headLine.backgroundColor=MAIN_BACK_COLOR;
     setViewHeight_Add(mainView,kmargin*8);
    
 /***** 姓名 *****/
    // 姓名
    UIView *nameView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    nameView.backgroundColor=WhiteColor;
    [mainView addSubview:nameView];

    UILabel *nameLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Name", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [nameView addSubview:nameLeftLabel];
    [nameLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(nameView.mas_centerY);
    }];
    
    UILabel *nameRightLabel=[mainView getLabelWithText:_doctorInfoDic[@"name"] font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [nameView addSubview:nameRightLabel];
    [nameRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(nameView.mas_centerY);
    }];
    
    UIView *nameLine=[UIView new];
    [nameView addSubview:nameLine];
    [nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    nameLine.backgroundColor=MAIN_BACK_COLOR;
    
   setViewHeight_Add(mainView,cellH);
    
/***** 从业年份 *****/
    // 从业年份
    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    timeView.backgroundColor=WhiteColor;
    [mainView addSubview:timeView];
    
    UILabel *timeLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Working Years", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [timeView addSubview:timeLeftLabel];
    [timeLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(timeView.mas_centerY);
    }];
    
    
    NSString *years;
    if (isCN == YES) {//.
        years = [NSString stringWithFormat:@"%@年",_doctorInfoDic[@"congye_times"]];
    } else{
        years = [NSString stringWithFormat:@"%@ year",_doctorInfoDic[@"congye_times"]];
    }
    UILabel *timeRightLabel=[mainView getLabelWithText:years font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [timeView addSubview:timeRightLabel];
    [timeRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(timeView.mas_centerY);
    }];
    
    UIView *timeLine=[UIView new];
    [timeView addSubview:timeLine];
    [timeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    timeLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH);
    
/*****  就职医院 *****/
    // 就职医院
    UIView *jobHospitalView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    jobHospitalView.backgroundColor=WhiteColor;
    [mainView addSubview:jobHospitalView];
    
    UILabel *jobHospitalLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Clinic", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [jobHospitalView addSubview:jobHospitalLeftLabel];
    [jobHospitalLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(jobHospitalView.mas_centerY);
    }];
    
    
    NSString *clinic;
    if (isCN == YES)
    {
        clinic=_doctorInfoDic[@"shop_cn_name"];
    }
    else
    {
        clinic=_doctorInfoDic[@"shop_en_name"];
    }
    UILabel *jobHospitalRightLabel=[mainView getLabelWithText:clinic font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//_doctorInfoDic[@"shop_cn_name"]
    [jobHospitalView addSubview:jobHospitalRightLabel];
    [jobHospitalRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(jobHospitalView.mas_centerY);
    }];
    
    UIView *jobHospitalLine=[UIView new];
    [jobHospitalView addSubview:jobHospitalLine];
    [jobHospitalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    jobHospitalLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH);
    
/*****  职称 *****/
    // 职称
    UIView *jobView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    jobView.backgroundColor=WhiteColor;
    [mainView addSubview:jobView];
    
    UILabel *jobLabel=[mainView getLabelWithText:NSLocalizedString(@"Title", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [jobView addSubview:jobLabel];
    [jobLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    

     NSString *jobStr;
    // 中文情况下
    if (isCN == YES)
    {
        if ([_doctorInfoDic[@"title_cn"] isEqualToString:@""])
        {
            jobStr=@"暂无";
        }
        else
        {
            jobStr=_doctorInfoDic[@"title_cn"];
        }
    }
    // 英文情况下
    else
    {
        if ([_doctorInfoDic[@"title_en"] isEqualToString:@""])
        {
            jobStr=@"no";
        }
        else
        {
            jobStr=_doctorInfoDic[@"title_en"];
        }
    }
    
    UILabel *jobTitleLabel=[mainView getLabelWithText:jobStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [jobView addSubview:jobTitleLabel];
    [jobTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(jobLabel.mas_left);
        make.top.mas_equalTo(jobLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *jobLine=[UIView new];
    [jobView addSubview:jobLine];
    [jobLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    jobLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH*3/2);

/*****  专业擅长 *****/
    // 专业擅长
    UIView *skillView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    skillView.backgroundColor=WhiteColor;
    [mainView addSubview:skillView];
    
    UILabel *skillLabel=[mainView getLabelWithText:NSLocalizedString(@"Speciality", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [skillView addSubview:skillLabel];
    [skillLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
    NSString *skillStr;
    // 中文情况下
    if (isCN == YES)
    {
        if ([_doctorInfoDic[@"speciality_cn"] isEqualToString:@""])
        {
            skillStr=@"暂无";
        }
        else
        {
            skillStr=_doctorInfoDic[@"speciality_cn"];
        }
    }
    // 英文情况下
    else
    {
        if ([_doctorInfoDic[@"speciality_en"] isEqualToString:@""])
        {
            skillStr=@"no";
        }
        else
        {
            skillStr=_doctorInfoDic[@"speciality_en"];
        }
    }
    UILabel *skillTitleLabel=[mainView getLabelWithText:skillStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [skillView addSubview:skillTitleLabel];
    [skillTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(skillLabel.mas_left);
        make.top.mas_equalTo(skillLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *skillLine=[UIView new];
    [skillView addSubview:skillLine];
    [skillLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    skillLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH*3/2);
    
/*****  简介 *****/
    // 简介
    UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    profileView.backgroundColor=WhiteColor;
    [mainView addSubview:profileView];
    
    UILabel *profileLabel=[mainView getLabelWithText:NSLocalizedString(@"Introduction", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [profileView addSubview:profileLabel];
    [profileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    

    NSString *profileStr;
    // 中文情况下
    if (isCN == YES)
    {
         if ([_doctorInfoDic[@"cn_descript"] isEqualToString:@""])
         {
              profileStr=@"暂无";
         }
        else
        {
             profileStr=_doctorInfoDic[@"cn_descript"];
        }
    }
    // 英文情况下
    else
    {
        if ([_doctorInfoDic[@"en_descript"] isEqualToString:@""])
        {
            profileStr=@"no";
        }
        else
        {
            profileStr=_doctorInfoDic[@"en_descript"];
        }
    }
    UILabel *profileTitleLabel=[mainView getLabelWithText:profileStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [profileView addSubview:profileTitleLabel];
    [profileTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(profileLabel.mas_left);
        make.top.mas_equalTo(profileLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *profileLine=[UIView new];
    [profileView addSubview:profileLine];
    [profileLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    profileLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH*3/2+kmargin*3/2);
    
/***** 执照号 *****/
    // 执照号
    UIView *codeView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    codeView.backgroundColor=WhiteColor;
    [mainView addSubview:codeView];
    
    UILabel *codeLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Medical License NO.", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [codeView addSubview:codeLeftLabel];
    [codeLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(codeView.mas_centerY);
    }];
    
    NSString *documentStr;
    if ([_doctorInfoDic[@"document_number"] isEqualToString:@""])
    {
       
        if (isCN == YES)
        {
             documentStr=@"暂无";
        }
        else
        {
             documentStr=@"no";
        }
        
    }
    else
    {
        documentStr=_doctorInfoDic[@"document_number"];
    }
    
    UILabel *codeRightLabel=[mainView getLabelWithText:documentStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [codeView addSubview:codeRightLabel];
    [codeRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(codeView.mas_centerY);
    }];
    
    UIView *codeLine=[UIView new];
    [codeView addSubview:codeLine];
    [codeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    codeLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,cellH);
    
/*****  我的医生执照 *****/
    // 我的医生执照
    UIView *photoView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3)];
    photoView.backgroundColor=WhiteColor;
    [mainView addSubview:photoView];
    
    UILabel *photoLabel=[mainView getLabelWithText:NSLocalizedString(@"Medical License", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [photoView addSubview:photoLabel];
    [photoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
    NSArray *doctorPhoto=[_doctorInfoDic[@"auth_img"] componentsSeparatedByString:@","];
    
    UIImageView *photoURL=[[UIImageView alloc]initWithFrame:CGRectMake(kmargin,40+kmargin,160/2,160/2)];
    [photoURL sd_setImageWithURL:[NSURL URLWithString:doctorPhoto[0]] placeholderImage:[UIImage imageNamed:@"photo"]];
    [photoView addSubview:photoURL];
    
    setViewHeight_Add(mainView,cellH*3+kmargin);
    
    UIView *bottomView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*6)];
    bottomView.backgroundColor=WhiteColor;
    [mainView addSubview:bottomView];
    
    UIButton *reviewBtn=[[UIButton alloc]initWithFrame:CGRectMake(kmargin*2,kmargin,APPW-kmargin*4,40)];
    [bottomView addSubview:reviewBtn];
    reviewBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(reviewBtn,kmargin/2,1,ClearColor);
    [reviewBtn setTitleColor:WhiteColor forState:0];
    [reviewBtn addTarget:self action:@selector(reviewBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    // 审核中
    if ([_auth_statusStr isEqualToString:@"1"])
    {

        [reviewBtn setTitle:NSLocalizedString(@"Under reviewing", nil) forState:0];//@"审核中/"审核中(24小时内审批)"
        reviewBtn.backgroundColor=ColorCECECE;
    }
    // 认证成功
    else  if ([_auth_statusStr isEqualToString:@"2"])
    {
        // 认证成功
        [reviewBtn setTitle:NSLocalizedString(@"gotoHomepage", nil) forState:0];//@"跳到首页"
        reviewBtn.backgroundColor=Main_Color;
    }
    // 认证失败
    else  if ([_auth_statusStr isEqualToString:@"3"])
    {
        [reviewBtn setTitle:@"重新认证" forState:0];
        reviewBtn.backgroundColor=ColorCECECE;
    }
    
    setViewHeight_Add(mainView,kmargin*6);

    return mainView;
}

-(void)reviewBtnEvent:(UIButton *)sender
{
    // 认证失败
    if ([_auth_statusStr isEqualToString:@"3"])
    {
        // 重新去认证
        ConfirmVC *confirmVC=[ConfirmVC new];
        [self.navigationController pushViewController:confirmVC animated:NO];
    }
    
    else if ([_auth_statusStr isEqualToString:@"2"])
    {
        // 认证成功 －－－－》点击跳到首页去
            [Util saveObjectToUserDefault:@"YES" forKey:HasConfirmDone];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(.1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
             AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
             QBMainVC *mainVC=[QBMainVC new];
             delegate.window.rootViewController=mainVC;
            
        });
    }
    // 认证中
    else
    {
        sender.enabled=NO;
    }
}


@end
